package com.prize.service.impl;

import com.prize.dtos.ResponseResult;
import com.prize.mapper.LevelMapper;
import com.prize.pojo.member.Level;
import com.prize.service.LevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @descriptions: 等级业务
 * @author: yunqiang
 * @date: 2022/5/28 22:46
 */
@Service
public class LevelServiceImpl implements LevelService {


    @Autowired
    private LevelMapper levelMapper;

    /**
     * 根据条件分页查询等级列表
     * @param levelName 等级名称
     * @param pageSize 条数
     * @param pageNow 当前页
     * @return
     */
    @Override
    public ResponseResult getLevels(String levelName, int pageSize, int pageNow) {
        if (pageNow > 0){
            pageNow  =(pageNow - 1)*pageSize;
        }else{
            pageNow = 0;
        }
        long total = levelMapper.getLevelCount(levelName);
        List<Level> levels = levelMapper.getLevels(levelName,pageNow,pageSize);
        if(CollectionUtils.isEmpty(levels)){
            return ResponseResult.noResult();
        }
        return ResponseResult.success(total,levels);
    }


    /**
     * 全量等级
     * @return
     */
    @Override
    public ResponseResult allLevel() {
        List<Level> levels = levelMapper.selectList(null);
        if(CollectionUtils.isEmpty(levels)){
            return ResponseResult.noResult();
        }
        return ResponseResult.successResult(levels);
    }

    /**
     * 新增等级
     * @param level
     * @return
     */
    @Override
    public ResponseResult addLevel(Level level) {
        level.setCreateTime(new Date());
        levelMapper.insert(level);
        return ResponseResult.success();
    }

    /**
     * 根据等级id修改等级
     * @param level
     * @return
     */
    @Override
    public ResponseResult updateLevel(Level level) {
        if(level.getId()==null){
            return  ResponseResult.error(505,"等级id为空!");
        }
        levelMapper.updateById(level);
        return ResponseResult.success();
    }


    /**
     * 根据等级id删除等级
     * @param id
     * @return
     */
    @Override
    public ResponseResult delLevel(Integer id) {
        levelMapper.deleteById(id);
        return ResponseResult.success();
    }
}
